package com.google.rui.imagezoom.test;

import android.graphics.Matrix;
import android.graphics.PointF;
import android.util.FloatMath;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;

public class ImageOnTouch implements OnTouchListener {

	// 放大缩小
	private Matrix matrix = new Matrix();
	private Matrix savedMatrix = new Matrix();

	private PointF start = new PointF();
	private PointF mid = new PointF();
	private float oldDist;
	private static final int NONE = 0;
	private static final int DRAG = 1;
	private static final int ZOOM = 2;
	private int mode = NONE;

	float[] mMatrixValues = new float[9];
	
	@Override
	public boolean onTouch(View v, MotionEvent event) {
		ImageView myImageView = (ImageView) v;
		myImageView.setScaleType(ScaleType.MATRIX);
		switch (event.getAction() & MotionEvent.ACTION_MASK) {
		// 设置拖拉模式
		case MotionEvent.ACTION_DOWN:
			matrix.set(myImageView.getImageMatrix());
			showMaixValues(matrix);
		
			
			matrix.set(myImageView.getImageMatrix());
			savedMatrix.set(matrix);
			start.set(event.getX(), event.getY());
			mode = DRAG;
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_POINTER_UP:
			mode = NONE;
			break;

		// 设置多点触摸模式
		case MotionEvent.ACTION_POINTER_DOWN:
			oldDist = spacing(event);
			if (oldDist > 10f) {
				savedMatrix.set(matrix);
				midPoint(mid, event);
				mode = ZOOM;
			}
			break;
		// 若为DRAG模式，则点击移动图片
		case MotionEvent.ACTION_MOVE:
			if (mode == DRAG) {
				matrix.set(savedMatrix);
				matrix.postTranslate(event.getX() - start.x, event.getY()
						- start.y);
			}
			// 若为ZOOM模式，则点击触摸缩放
			else if (mode == ZOOM) {
				float newDist = spacing(event);
				if (newDist > 10f) {
					matrix.set(savedMatrix);
					float scale = newDist / oldDist;
					// 设置硕放比例和图片的中点位置
					matrix.postScale(scale, scale, mid.x, mid.y);
					System.out.println("放大倍数："+getScale());
					
				}
			}
			break;
		}
		myImageView.setImageMatrix(matrix);
		return true;
	}

	// 计算移动距离
	private float spacing(MotionEvent event) {
		float x = event.getX(0) - event.getX(1);
		float y = event.getY(0) - event.getY(1);
		return FloatMath.sqrt(x * x + y * y);
	}

	// 计算中点位置
	private void midPoint(PointF point, MotionEvent event) {
		float x = event.getX(0) + event.getX(1);
		float y = event.getY(0) + event.getY(1);
		point.set(x / 2, y / 2);
	}

    public void showMaixValues(Matrix matrix){
    	float f[]=new float[9];
    	        
    	        matrix.getValues(f);
    	        System.out.println(matrix.getClass());
    	for(int i=1;i<=3;i++){
    		for(int j=1;j<=3;j++)

    		System.out.print(f[i*j-1]+"\t");
    		System.out.println("");
    	}
    	
    }
    
    protected float getValue(Matrix matrix, int whichValue)
	{
		matrix.getValues(mMatrixValues);
		return mMatrixValues[whichValue];
	}

	// Get the scale factor out of the matrix.
	protected float getScale(Matrix matrix)
	{
		return getValue(matrix, Matrix.MSCALE_X);
	}

	protected float getScale()
	{
		return getScale(matrix);
	}
    
}
